<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    v-drag
  >
    <template slot="footer">
      <a-button @click="handleCancel">{{$t('关闭')}}</a-button>
    </template>
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('业务时间')">
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.buyTime_begin"
              ></j-date>
              <span>~</span>
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.buyTime_end"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('商品名称')">
              <a-input :placeholder="$t('请输入商品名称')" v-model="queryParam.goodsInfo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('查询')}}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{$t('重置')}}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
              <a-button type="primary" @click="handleExportXls($t('抽奖记录'))" style="margin-left: 8px">{{$t('导出')}}</a-button>
            </span>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('设备')">
                <a-select show-search :filter-option="false" @search="handleSearch" v-model="queryParam.deviceId">
                  <a-select-option :value="item.id" :key="index" v-for="(item, index) in deviceList">
                    <span>{{ item.name }}</span>
                    <span v-if="item.name != $t('全部')">({{ item.id }})</span>
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('业务渠道')">
                <a-select v-model="queryParam.deviceType">
                  <a-select-option :value="item.value" :key="index" v-for="(item, index) in deviceinfoList">{{
                    item.title
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="4" :lg="5" :md="6" :sm="24">
              <a-form-item :label="$t('中奖状态')">
                <a-select v-model="queryParam.state">
                  <a-select-option :value="item.value" :key="index" v-for="(item, index) in stateList">{{
                    item.title
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('订单号')">
                <a-input :placeholder="$t('请输入订单号')" v-model="queryParam.id"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('会员信息')">
                <a-input :placeholder="$t('请输入手机号/卡号/姓名')" v-model="queryParam.memberInfoSearch"></a-input>
              </a-form-item>
            </a-col>
          </template>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="footer">
          <div>
            <span>{{$t('中奖次数')}}：{{ totalInfo.win }}</span
            ><span style="margin-left: 10px">{{$t('未中奖次数')}}：{{ totalInfo.notWin }}</span>
          </div>
        </template>
      </a-table>
    </div>
  </a-modal>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions, filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { getAction, downFile } from '@/api/manage'

export default {
  name: 'DrawRecordModal',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      queryParam: {
        column: 'createTime',
        order: 'desc',
        createTime_begin: '',
        createTime_end: '',
        goodsInfo: '',
        deviceType: '',
        deviceId: '',
        id: '',
        memberInfo: '',
        state: ''
      },
      // 表头
      columns: [
        {
          title: this.$t('订单号'),
          align: 'center',
          dataIndex: 'id',
        },
        {
          title: this.$t('抽奖时间'),
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: this.$t('手机号'),
          align: 'center',
          dataIndex: 'memberPhone',
        },
        {
          title: this.$t('会员卡号'),
          align: 'center',
          dataIndex: 'chipNo',
        },
        {
          title: this.$t('姓名'),
          align: 'center',
          dataIndex: 'memberName',
        },
        {
          title: this.$t('中奖状态'),
          align: 'center',
          dataIndex: 'state_dictText',
        },
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsId_dictText',
        },
        {
          title: this.$t('业务渠道'),
          align: 'center',
          dataIndex: 'deviceType_dictText',
        },
        {
          title: this.$t('设备名称'),
          align: 'center',
          dataIndex: 'deviceId_dictText',
        },
        {
          title: this.$t('设备编码'),
          align: 'center',
          dataIndex: 'deviceId',
        },
      ],
      url: {
        list: '/activity/bisMemberDrawUse/list',
        exportXlsUrl: '/activity/bisMemberDrawUse/exportXls',
        totalUrl: '/activity/bisMemberDrawUse/total'
      },
      dictOptions: {},
      title: this.$t('抽奖记录'),
      width: 1500,
      visible: false,
      confirmLoading: false,
      deviceinfoList: [],
      deviceList: [],
      stateList: [{
        value:'',
        title:this.$t('全部')
      },{
        value:'1',
        title:this.$t('是')
      },{
        value:'0',
        title:this.$t('否')
      }],
      activityId: '',
      isCustom: true,
      isfooter: true,
      totalInfo: {
        win: 0,
        notWin: 0,
      },
    }
  },
  created() {},
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      initDictOptions('bis_device_info.type').then((res) => {
        if (res.success) {
          this.deviceinfoList = res.result
          this.deviceinfoList.unshift({ value: '', title: this.$t('全部') })
        }
      })
      this.handleSearch('')
    },
    handleSearch(value) {
      let query = {}
      if (value) {
        query.deviceInfo = value
      }
      getAction('/device/bisDeviceInfo/list', query).then((res) => {
        if (res.success) {
          this.deviceList = res.result.records
          this.deviceList.unshift({ id: '', name: this.$t('全部') })
        }
      })
    },
    show(record) {
      this.visible = true
      this.queryParam= {
        column: 'createTime',
        order: 'desc',
        createTime_begin: '',
        createTime_end: '',
        goodsInfo: '',
        deviceType: '',
        deviceId: '',
        id: '',
        memberInfo: '',
        state: '',
        activityId: record.id
      }
      this.activityId = record.id
      this.loadData(1)
    },
    searchQuery() {
      this.loadData(1)
    },
    handleCancel() {
      this.visible = false
    },
    searchReset() {
      this.queryParam = {
        column: 'createTime',
        order: 'desc',
        createTime_begin: '',
        createTime_end: '',
        goodsInfo: '',
        deviceType: '',
        deviceId: '',
        id: '',
        memberInfo: '',
        state: '',
        activityId: this.activityId,
      }
      this.loadData(1)
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>